package TUI;

import ControllerLayer.*;
import ModelLayer.*;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;

/**
 * Write a description of class RentalUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RentalUI
{
    private RentalCtr rentalCtr;
    /**
     * Constructor for objects of class RentalUI
     */
    public RentalUI()
    {
        rentalCtr = RentalCtr.getInstance();
    }
    
    public void start()
    {
        clr();
        rentalMenu();
    }
    
    private void rentalMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeRentalMenu();
            switch(choise)
            {
                case 1:
                    createRental();
                    break;
                case 2:
                    showRentalList();
                    break;
                case 3:
                    exit = true;
                    break;
                default:
                    break;
            }
        }
    }
    
    private int writeRentalMenu()
    {
        print(" *** Udlejning Menu *** ");
        print(" (1) Opret ny reservation");
        print(" (2) Vis liste over udlejninger og reservationer");
        print(" (3) Gaa tilbage til hovedmenu");
        print("\n Foretag din beslutning: ");
            
        boolean exit = false;
        int choise = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                choise = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt valg, proev igen");
            }
        }
        return choise;
    }
    
    private long getInputLong(String str)
    {
        print(str);
        
        boolean exit = false;
        long lon = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                lon = keyboard.nextLong();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return lon;
    }
    
    private int getInputInt(String str)
    {
        print(str);
        
        boolean exit = false;
        int integer = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                integer = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return integer;
    }
    
    private void createRental()
    {
        oneSpace();
        ArrayList<Customer> customerList  = rentalCtr.getCustomerList();
        for(Customer c : customerList)
        {
            print("ID: "  + c.getId() + " Navn: " + c.getName());
        }
        oneSpace();
        long customerID = getInputLong("Indtast id nummeret paa kunden:");
        Customer customer = null;
        try
        {
            customer = (Customer)rentalCtr.getPerson(customerID);
        }
        catch(Exception e)
        {
        }
        oneSpace();
        ArrayList<ToolDescription> descriptionList = rentalCtr.getToolDescriptionList();
        for(ToolDescription td : descriptionList)
        {
            print("ID: " + td.getDescriptionID() + " Beskrivelse: " + td.getDescription());
        }
        oneSpace();
        long descriptionID = getInputLong("Indtast id nummeret paa vaerktoejet: ");
        ToolDescription toolDescription = null;
        try
        {
            toolDescription = rentalCtr.getToolDescription(descriptionID);
        }
        catch(Exception e)
        {
        }
        int toStartYear = getInputInt("Indtast hvilket aar lejeperioden skal starte:");
        int toStartMonth = getInputInt("Indtast hvilken maaned lejeperioden skal starte:");
        int toStartDay = getInputInt("Indtast hvilken dag i maaneden lejeperioden skal starte:");
        int toEndYear = getInputInt("Indtast hvilkent aar lejeperioden skal slutte:");
        int toEndMonth = getInputInt("Indtast hvilken maaned lejeperioden skal slutte:");
        int toEndDay = getInputInt("Indtast hvilken dag i maaneden lejeperioden skal slutte:");
        Date toStartDate = new Date(toStartYear, toStartMonth, toStartDay);
        Date toEndDate = new Date(toEndYear,toEndMonth,toEndDay);
        Date reservationDate = Calendar.getInstance().getTime();
        doubleSpace();
        
        try
        {
            rentalCtr.createRental(customer, new Employee(0,"na","na","na","na","na"),toolDescription,toStartDate,toEndDate,reservationDate,Rental.RESERVED);
        }
        catch(Exception e)
        {
            print("Kunne ikke finde ønskede kunde eller værktøj.");
        }
    }
    
    private void showRentalList()
    {
        oneSpace();
        ArrayList<Rental> rentalList = rentalCtr.getRentalList();
        for(Rental r : rentalList)
        {
            print("ID: " + r.getID());
            print("Kunde:     ID: " + r.getCustomer().getId() + " Navn: " + r.getCustomer().getName());
            print("Ekspedien: ID: " + r.getEmployee().getId() + " Navn: " + r.getEmployee().getName());
            print("Vaerktoejstype Id: " + r.getToolDescription().getDescriptionID() + " Beskrivelse: " + r.getToolDescription().getDescription());
            print("Reserveret fra " + r.getToStartDate().toString() + "\n Reserveret til " + r.getToEndDate().toString());
            print("Reserveret den " + r.getReservationDate().toString());
        }
        oneSpace();
    }
    
    /**
     * This methid prints a sting to the terminal
     * 
     * @param   str A sting to be printed to the terminal.
     */
    private void print(String str)
    {
        System.out.println(str);
    }
    
    /**
     * This method clears the terminal.
     */
    private void clr()
    {
        print("\f");
    }
    
        private void oneSpace()
    {
        print("\n");
    }
    
    private void doubleSpace()
    {
       print("\n\n");
    }
}
